package org.locker.service.impl;

import cn.hutool.core.util.ObjectUtil;
import org.locker.common.core.exception.ServiceException;
import org.locker.common.core.utils.MapstructUtils;
import org.locker.common.core.utils.StringUtils;
import org.locker.common.mybatis.core.page.TableDataInfo;
import org.locker.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.locker.domain.ManagerConfig;
import org.locker.domain.bo.ManagerConfigBo;
import org.locker.domain.vo.ManagerConfigVo;
import org.locker.mapper.ManagerConfigMapper;
import org.locker.service.IManagerConfigService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 场地分润配置Service业务层处理
 *
 * @author winnie
 * @date 2023-11-05
 */
@RequiredArgsConstructor
@Service
public class ManagerConfigServiceImpl  extends ServiceImpl<ManagerConfigMapper, ManagerConfig>  implements IManagerConfigService {

    private final ManagerConfigMapper baseMapper;

    /**
     * 查询场地分润配置
     */
    @Override
    public ManagerConfigVo queryById(Long managerConfigId){
        return baseMapper.selectVoById(managerConfigId);
    }

    /**
     * 查询场地分润配置列表
     */
    @Override
    public TableDataInfo<ManagerConfigVo> queryPageList(ManagerConfigBo bo, PageQuery pageQuery) {
        Page<ManagerConfigVo> result = baseMapper.listByPage(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询场地分润配置列表
     */
    @Override
    public List<ManagerConfigVo> queryList(ManagerConfigBo bo) {
        LambdaQueryWrapper<ManagerConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ManagerConfig> buildQueryWrapper(ManagerConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ManagerConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPositionId() != null, ManagerConfig::getPositionId, bo.getPositionId());
        lqw.eq(bo.getManagerId() != null, ManagerConfig::getManagerId, bo.getManagerId());
        lqw.eq(bo.getParentManagerId() != null, ManagerConfig::getParentManagerId, bo.getParentManagerId());
        lqw.eq(StringUtils.isNotBlank(bo.getShareType()), ManagerConfig::getShareType, bo.getShareType());
        lqw.eq(bo.getFees() != null, ManagerConfig::getFees, bo.getFees());
        lqw.eq(bo.getRate() != null, ManagerConfig::getRate, bo.getRate());
        lqw.eq(StringUtils.isNotBlank(bo.getParentShareType()), ManagerConfig::getParentShareType, bo.getParentShareType());
        lqw.eq(bo.getParentRate() != null, ManagerConfig::getParentRate, bo.getParentRate());
        lqw.eq(bo.getParentFees() != null, ManagerConfig::getParentFees, bo.getParentFees());
        return lqw;
    }

    /**
     * 新增场地分润配置
     */
    @Override
    public Boolean insertByBo(ManagerConfigBo bo) {
        ManagerConfig add = MapstructUtils.convert(bo, ManagerConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setManagerConfigId(add.getManagerConfigId());
        }
        return flag;
    }

    /**
     * 修改场地分润配置
     */
    @Override
    public Boolean updateByBo(ManagerConfigBo bo) {
        ManagerConfig update = MapstructUtils.convert(bo, ManagerConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ManagerConfig entity){
        //做一些数据校验,如唯一约束
        Long managerConfigId = entity.getManagerConfigId();
        Long positionId = entity.getPositionId();
        LambdaQueryWrapper<ManagerConfig> wrapper = Wrappers.<ManagerConfig>lambdaQuery().eq(ManagerConfig::getPositionId, positionId);
        if (ObjectUtil.isNull(managerConfigId)) {
            wrapper.ne(ManagerConfig::getManagerConfigId,managerConfigId);
        }
        long count = this.count(wrapper);
        if (count > 0){
            throw new ServiceException("该场地已存在分润配置,请核实后再添加!");
        }

    }

    /**
     * 批量删除场地分润配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
